﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace Du_Lich
{
    public partial class NghienCuuThiTruongForm : Form
    {
        public NghienCuuThiTruongForm()
        {
            InitializeComponent();
        }

        string strConn = ConfigurationManager.ConnectionStrings["DuLich"].ConnectionString;
        SqlConnection conn = null;
        SqlCommand cmd = null;
        SqlDataAdapter da = null;

        public void MoKetNoi()
        {
            conn = new SqlConnection(strConn);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
        }

        public void DongKetNoi()
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }

        public DataTable LoadDS()
        {
            string sqlLoad = "Select * from NGHIENCUU";
            da = new SqlDataAdapter(sqlLoad, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;

        }

        private void NghienCuuThiTruongForm_Load(object sender, EventArgs e)
        {
            MoKetNoi();
            gvNghienCuuThiTruong.DataSource = LoadDS();

            cbManv.DataSource = LoadMaNV();
            cbManv.DisplayMember = "MaNV";
        }

        private void NghienCuuThiTruongForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DongKetNoi();
        }

        public void Them(string manc,string tinhtp, string noikhaosat,string tungay,string denngay,string manv)
        {
            string strInsert="Insert NGHIENCUU values(@manc,@tinhtp,@noiks,@tungay,@denngay,@manv)";
            cmd = new SqlCommand(strInsert,conn);
            cmd.Parameters.AddWithValue("@manc", manc);
            cmd.Parameters.AddWithValue("@tinhtp", tinhtp);
            cmd.Parameters.AddWithValue("@noiks", noikhaosat);
            cmd.Parameters.AddWithValue("@tungay", tungay);
            cmd.Parameters.AddWithValue("@denngay", denngay);
            cmd.Parameters.AddWithValue("@manv", manv);

            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            
        }

        private int TimKiem()
        {
            string chuoitim = txtmanghiencuu.Text;
            int i = -1;
            foreach (DataGridViewRow dgv in gvNghienCuuThiTruong.Rows)
            {
                try
                {
                    i++;
                    if (string.Compare(chuoitim, dgv.Cells[0].Value.ToString()) == 0)
                        return i;
                }
                catch (Exception)
                {
                    MessageBox.Show("Ban nhap sai roi ! Nhap lai di");
                }
            }
            return -1;
        }

        public DataTable LoadMaNV()
        {
            string strManv = "select MaNV from NHANVIEN ";
            da = new SqlDataAdapter(strManv, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Them(txtmanghiencuu.Text, txtTinhTP.Text, txtnoikhaosat.Text, datetungay.Value.ToString(), datedenngay.Value.ToString(), cbManv.Text);
            gvNghienCuuThiTruong.DataSource = LoadDS();
        }

        public void Xoa(string manc)
        {
            string strDelete = "Delete NGHIENCUU where MaNghienCuu=@manc";
            cmd = new SqlCommand(strDelete, conn);
            cmd.Parameters.AddWithValue("@manc",manc);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private void gvNghienCuuThiTruong_SelectionChanged(object sender, EventArgs e)
        {
            if (gvNghienCuuThiTruong.SelectedRows.Count == 0)
                return;
            int i = gvNghienCuuThiTruong.SelectedRows[0].Index;
            txtmanghiencuu.Text = gvNghienCuuThiTruong.Rows[i].Cells[0].Value.ToString();
            txtTinhTP.Text = gvNghienCuuThiTruong.Rows[i].Cells[1].Value.ToString();
            txtnoikhaosat.Text = gvNghienCuuThiTruong.Rows[i].Cells[2].Value.ToString();
            datetungay.Value = DateTime.Parse(gvNghienCuuThiTruong.Rows[i].Cells[3].Value.ToString());
            datedenngay.Value = DateTime.Parse(gvNghienCuuThiTruong.Rows[i].Cells[4].Value.ToString());
            cbManv.Text = gvNghienCuuThiTruong.Rows[i].Cells[5].Value.ToString();

       }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int i = gvNghienCuuThiTruong.SelectedRows[0].Index;
            Xoa(gvNghienCuuThiTruong.Rows[i].Cells[0].Value.ToString());
            gvNghienCuuThiTruong.DataSource = LoadDS();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            int kq = TimKiem();
            if (kq == -1)
                MessageBox.Show("Khong tim thay", "Thong bao tim kiem");
            else
                gvNghienCuuThiTruong.Rows[kq].Selected = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DongKetNoi();
            this.Close();
        }
    }
}
